package nqueens.config;

import java.util.Map;
import java.util.Properties;

import nqueens.exception.ParameterMissingException;
import nqueens.util.Debugger;

public class GAProperties {

	private static GAProperties instance;

	public static final String MAX_GENERATIONS = "generations.max";

	public static final String POPULATION_SIZE = "population.size";

	public static final String NUM_QUEENS = "queens.number";

	public static final String MAX_SOLUTIONS = "solutions.max";

	public static final String CROSSOVER_PARAM = "crossover.number";
	
	public static final String MUTATION_RATE = "mutation.rate";

	private Properties props;

	private GAProperties() {
		props = new Properties();
	}

	public static GAProperties getInstance() {
		if (instance == null) {
			instance = new GAProperties();
		}
		return instance;
	}

	public String getString(String key) {
		if (!props.containsKey(key)) {
			throw new ParameterMissingException("Parameter <" + key
					+ "> is missing.");
		}
		return props.getProperty(key);
	}

	public Integer getInteger(String key) {
		return Integer.valueOf(getString(key));
	}

	public Double getDouble(String key) {
		return Double.valueOf(getString(key));
	}

	public void setString(String key, String value) {
		Debugger.getInstance().print(key + " set to " + value);
		props.setProperty(key, value);
	}

	public void setInteger(String key, int value) {
		setString(key, Integer.toString(value));
	}

	public void setDouble(String key, double value) {
		setString(key, Double.toString(value));
	}

	public void setAll(Map<String, String> info) {
		for (String key : info.keySet()) {
			setString(key, info.get(key));
		}
	}
}
